package hzk.codex2016.sort;

import java.util.Arrays;

public class Arrs {

	public static boolean isSorted(int[] arr) {
		if (arr == null)
			return true;
		for (int i = 0; i < arr.length - 1; i++) {
			if (arr[i] > arr[i + 1])
				return false;

		}
		return true;
	}

	public static final int RAND_MAX = 3000;
	public static final int RAND_MIN = 0;

	public static int[] randomIntArr(int size) {
		return randomIntArr(size, RAND_MAX);
	}

	public static int[] randomIntArr(int size, int max) {
		int[] ret = new int[size];
		for (int i = 0; i < size; ++i) {
			ret[i] = (int) (Math.random() * (max - RAND_MIN) + RAND_MIN);
		}
		return ret;
	}

	public static void test(Sorter sorter, int testArrSize) {
		test(sorter, testArrSize, RAND_MAX, false);
	}

	public static void test(Sorter sorter, int testArrSize, int max) {
		test(sorter, testArrSize, max, false);
	}

	public static void test(Sorter sorter, int testArrSize, int max, boolean printDetail) {
		int[] a = randomIntArr(testArrSize, max);
		if (printDetail)
			System.out.printf("----test (size: %d) ---\n", testArrSize);
		if (printDetail)
			System.out.println(Arrays.toString(a));
		long t1 = System.currentTimeMillis();
		sorter.sort(a);
		long t2 = System.currentTimeMillis();
		if (printDetail)
			System.out.println(Arrays.toString(a));
		System.out.printf("size: %d, time used: %d ms, result is %s\n", testArrSize, (t2 - t1),
				isSorted(a) ? "Good." : "Bad!!!!!!");

	}

	public static void testJdkArrays(int testArrSize, int max) {
		test(new Sorter() {

			@Override
			public void sort(int[] arr) {
				Arrays.sort(arr);
			}

		}, testArrSize, max);
	}

}
